#include "LoadingClientCollection.hpp"

LoadingClientCollection* LoadingClientCollection::instance = 0;

/** 
* Initializes an empty LoadingClientCollection
*/
LoadingClientCollection::LoadingClientCollection(){
    clientCount = 0;
    clientList = 0;
}

/** 
* Adds the client to loading list.
* @param client The client socket to add
*/
void LoadingClientCollection::addClient(const sf::IpAddress& client, const unsigned short port){
	LoadingClientNode* newClient = new LoadingClientNode();
	newClient->next = clientList;
	newClient->address = new sf::IpAddress(client);
    newClient->port = port;
	newClient->timeout = new sf::Clock();

	if(clientList != 0){
		clientList->prev = newClient;
	}
	clientList = newClient;
	clientList->prev = 0;
	clientCount++;
}

/** 
* Removes the client from the list.
* @param client The client to remove
*/
void LoadingClientCollection::removeClient(LoadingClientNode* client){
	if(client->prev == 0){
		clientList = client->next;
		if(clientList != 0){
			clientList->prev = 0;
		}
	}else{
		client->prev->next = client->next;
		if(client->next != 0){
			client->next->prev = client->prev;
		}
	}

	delete client;
	clientCount--;
}

/** 
* Gets the client count.
* @return Number of clients in loading client list
*/
int LoadingClientCollection::getClientCount(){
    return clientCount;
}
